package com.hanson.api;

import com.hanson.common.bean.ResultMessage;
import com.hanson.common.util.BusinessUtil;
import com.hanson.service.GoodsCollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@Api(tags = "商品收藏管理")
@RestController
@RequestMapping("/api/cart")
public class CollectApiController {
    @Autowired
    private GoodsCollectService goodsCollectService;

    @ApiOperation(value = "添加商品到收藏")
    @RequestMapping(value = "/add/{goodsId}", method = RequestMethod.POST)
    public ResultMessage add(@PathVariable String goodsId, HttpServletRequest request) {
        String openId = BusinessUtil.getHeaderOpenId(request);
        goodsCollectService.add(goodsId, openId);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setMsg("添加收藏成功！");
        return resultMessage;
    }

    @ApiOperation(value = "取消商品收藏")
    @RequestMapping(value = "/delete/{goodsId}", method = RequestMethod.POST)
    public ResultMessage delete(@PathVariable String goodsId, HttpServletRequest request) {
        String openId = BusinessUtil.getHeaderOpenId(request);
        goodsCollectService.delete(openId, Integer.parseInt(goodsId));
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setMsg("删除收藏成功！");
        return resultMessage;
    }

    @ApiOperation(value = "查询商品收藏列表")
    @RequestMapping(value = "/myCollect", method = RequestMethod.GET)
    public ResultMessage myCollect(HttpServletRequest request) throws Exception {
        String openId = BusinessUtil.getHeaderOpenId(request);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setData(goodsCollectService.selectByOpenId(openId));
        return resultMessage;
    }
}
